package cn.jsj.cxsys.controller;

import org.springframework.stereotype.*;
import cn.jsj.cxsys.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.jsj.cxsys.controller.packageClass.*;
import org.springframework.web.bind.annotation.*;
import cn.jsj.cxsys.pojo.*;
import javax.servlet.http.*;
import org.apache.commons.lang.time.*;
import java.text.*;
import java.util.*;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.*;

@ResponseBody
@CrossOrigin
@Controller
public class RecordController
{
    @Autowired
    private RecordService recordService;
    @Autowired
    SimpleDateFormat simpleDateFormat;
    @Value("${superinformation.account}")
    private String superAccount;
    @Value("${superinformation.password}")
    private String superPassword;
    @Value("${superinformation.recorddumptime}")
    private int dumpRecordTime;
    public static final String FILE_NAME = "src/main/resources/static/record.xlsx";

    @RequestMapping(value = { "superGetRecord" }, method = { RequestMethod.POST }, params = { "account", "password", "page", "rows" })
    public EasyUIPages<Record> superGetRecord(@RequestParam("account") final String account, @RequestParam("password") final String password, @RequestParam("page") final int page, @RequestParam("rows") final int rows) {
        final List<Record> tt = (List<Record>)this.recordService.getAllRecords(account, password, page, Integer.MAX_VALUE);
        if (tt != null) {
            Collections.sort(tt);
        }
        final int recordNumber = this.recordService.getNumberOfRecord();
        return new EasyUIPages<Record>(recordNumber, tt);
    }

    @RequestMapping(value = { "teacherGetRecord" }, method = { RequestMethod.POST })
    public List<Record> teacherGetRecord(@RequestBody final DinacSearchRecord ds) {
        if (ds == null) {
            return null;
        }
        return (List<Record>)this.recordService.getRecordsOfOneComputer(ds.getTeacher(), ds.getPageNum(), ds.getPageSize(), ds.getStudentAccount(), ds.getTeacherAccount(), ds.getStartTime(), ds.getOverTime(), ds.getLabID(), ds.getLabNum(), ds.getPcID());
    }

    @RequestMapping(value = { "Top" }, method = { RequestMethod.POST, RequestMethod.GET })
    public List<Singin> getTop10() {
        return (List<Singin>)this.recordService.getTop10Singin();
    }

    @RequestMapping(value = { "TopCore" }, method = { RequestMethod.POST, RequestMethod.GET })
    public List<Singin> getCoreTop10() {
        return (List<Singin>)this.recordService.getSinginCore();
    }

    @RequestMapping(value = { "downloadRecord" }, method = { RequestMethod.POST }, params = { "labNum", "startTime", "limitTime" })
    public void downLoad(@RequestParam("labNum") final String labNum, @RequestParam("startTime") String startTime, @RequestParam("limitTime") String limitTime, final HttpSession session, final HttpServletRequest request, final HttpServletResponse response) {
        final SimpleDateFormat sim = new SimpleDateFormat("MM/dd/yyyy HH:mm");
        final Date now = new Date();
        if (startTime == null || startTime.trim().length() == 0) {
            final Date startDate = DateUtils.addDays(now, -1 * this.dumpRecordTime);
            startTime = this.simpleDateFormat.format(startDate);
        }
        else {
            try {
                final Date startDate = sim.parse(startTime.trim());
                startTime = this.simpleDateFormat.format(startDate);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (limitTime == null || limitTime.trim().length() == 0) {
            final Date limitDate = new Date();
            limitTime = this.simpleDateFormat.format(limitDate);
        }
        else {
            try {
                final Date limitDate = sim.parse(limitTime.trim());
                limitTime = this.simpleDateFormat.format(limitDate);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<Record> storage = (List<Record>)this.recordService.downLoadRecord(labNum, this.superAccount, this.superPassword, startTime, limitTime);
        if (storage == null) {
            storage = new LinkedList<Record>();
        }
        Collections.sort(storage);
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        Workbook workbook = null;
        workbook = ExcelWriter.exportData(storage);
        ServletOutputStream out = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        try {
            outputStream = new FileOutputStream("record.xlsx");
            workbook.write((OutputStream)outputStream);
            outputStream.flush();
            outputStream.close();
            out = response.getOutputStream();
            inputStream = new FileInputStream("record.xlsx");
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream((OutputStream)out);
            final byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        }
        catch (FileNotFoundException e2) {
            e2.printStackTrace();
        }
        catch (IOException e3) {
            e3.printStackTrace();
        }
        finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                }
                catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                }
                catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                }
                catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
        }
    }
}
